<template>
  <div class="register-container">
    <el-card class="register-box">
      <template #header>
        <div class="title">
          <h2>用户注册</h2>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
        status-icon
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <div class="button-container">
            <el-button type="primary" :loading="loading" @click="handleRegister">
              注册
            </el-button>
            <el-button @click="$router.push('/login')">返回登录</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { registerAPI } from '../../api/user'

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在 3 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在 6 到 20 个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ], 
}

// 注册处理函数
const handleRegister = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const registerForm = {
          username: form.username,
          password: form.password,
        }
        const {data:res} = await registerAPI(registerForm)
        if(res.code !== 200){
          ElMessage.error(res.msg ? res.msg : '注册失败')
          loading.value = false
          return
        }
        ElMessage.success('注册成功')
        router.push('/login')
      } catch (error) {
        console.error('注册失败:', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;  /* 修改背景色，与登录页面保持一致 */
}

.register-box {
  width: 420px;
  background-color: rgba(255, 255, 255, 0.9);  /* 添加卡片背景透明度 */
  border-radius: 8px;         /* 添加圆角 */
}

.title {
  text-align: center;
}

.title h2 {
  color: #333;               /* 标题颜色 */
  margin: 0;
  padding: 10px 0;
}

.button-container {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.el-button {
  width: 120px;
  margin-top: 10px;
}

/* 添加输入框样式 */
:deep(.el-input__wrapper) {
  background-color: transparent;
}

:deep(.el-form-item__label) {
  color: #333;
}
</style>
